<template>
  <e-table
    ref="eTable"
    class="data-tale"
    @getList="handleGetList"
    :tableData="tableData"
    :columns="columns"
    :loading="loading"
    :total="total"
    :border="false"
    :rowKey="'moralityItemId'"
    :treeProps="{children: 'children', hasChildren: 'hasChildren'}"
  >
    <el-table-column slot="action" fixed="right" width="50" align="center">
      <template slot-scope="scope">
        <el-dropdown>
          <a class="el-dropdown-link">
            <i class="el-icon-menu"></i>
          </a>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item>
              <a @click="handleEdit(true, scope.row)">编辑</a>
            </el-dropdown-item>
            <el-dropdown-item>
              <a class="red" @click="handleDelete(scope.row)">删除</a>
            </el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </template>
    </el-table-column>
    <el-table-column label="项目类型" width="140" align="center" slot="type">
      <template slot-scope="scope">{{ scope.row.moralityItemType === 1 ? '基础项目' : '一票认定差项目' }}</template>
    </el-table-column>
    <el-table-column label="是否末级" width="80" align="center" slot="type">
      <template slot-scope="scope">{{ scope.row.leaf ? '是' : '否' }}</template>
    </el-table-column>
    <el-table-column label="排序" width="100" align="center" slot="type">
      <template slot-scope="scope">
        <el-input @blur="handleOrder(scope.row)" v-model.number="scope.row.itemOrder" />
      </template>
    </el-table-column>
  </e-table>
</template>

<script>
import { moralityPage, moralityAll, moralityDelete, moralityUpdate } from '@/api/morality'

import { openNotification } from '@/utils/util'
export default {
  data() {
    return {
      tableData: [],
      loading: false,
      total: 1,
      queryParam: {
        pageSize: 10,
        pageNo: 1
      },
      columns: [
        {
          label: '项目名称',
          prop: 'moralityItemName',
          align: 'left'
        },
        // { slot: 'name' },
        { slot: 'type' },
        { slot: 'leaf' },
        {
          label: '项目标准分',
          width: '100px',
          prop: 'itemStandard'
        },
        { slot: 'sort' },
        { slot: 'action' }
      ]
    }
  },
  mounted() {
    this.handleGetList()
  },
  methods: {
    async handleOrder(row) {
      if (row.itemOrder > 100 || row.itemOrder < 0) {
        this.$message.error('排序值限制0-100')
        return false
      }
      if (Object.is(row.itemOrder, NaN)) {
        this.$message.error('您输入不是一个数字')
        return false
      }
      const res = await moralityUpdate(row)
      if (res.state === '000000') {
        this.handleGetList()
      }
    },
    handleEdit(bool, obj) {
      this.$emit('edit', bool, obj)
    },
    handleData(arr1, arr2) {
      arr1.map(v => {
        v.children = []
        arr2.map(j => {
          if (v.moralityItemId === j.parentItemId) {
            v.children.push(j)
            if (v.children && v.children.length > 0) {
              this.handleData(v.children, arr2)
            }
          }
        })
      })
    },
    async handleGetList(param = {}) {
      this.loading = true
      const res = await moralityAll()
      if (res && res.data.length > 0) {
        const { state, data } = res
        let parentArr = []
        data.map(v => {
          if (!v.parentItemId) {
            parentArr.push(v)
          }
        })
        this.handleData(parentArr, data)
        this.tableData = parentArr
        console.log('tableData', this.tableData)
      } else {
        this.tableData = []
      }
      this.loading = false
    },
    handleDelete(row) {
      console.log('row', row)
      const _this = this
      this.$confirm({
        title: '删除操作',
        content: `您确定删除[${row.moralityItemName}]这条记录吗？`,
        okText: '确定',
        okType: 'danger',
        cancelText: '取消',
        centered: true,
        async onOk() {
          // 调用删除接口，刷新表格数据
          const res = await moralityDelete(row.moralityItemId, {})
          openNotification('success', '删除成功')
          const index = _this.tableData.findIndex(v => v.moralityItemId === row.moralityItemId)
          _this.tableData.splice(index, 1)
        }
      })
    }
  }
}
</script>
